var app = getApp();
// 可以在任意页面获取音频实例，实例唯一
const ba = wx.getBackgroundAudioManager();
export default {
  data() {
    return {
      list: [{
        id: 1,
        title: 'I lomo you',
        epname: 'I lomo you',
        singer: '王欣宇',
        src: 'http://music.163.com/song/media/outer/url?id=441441423.mp3',
        coverImgUrl: 'https://pic.imgdb.cn/item/63dff0f14757feff33257352.png'
      }, {
        id: 2,
        title: '告白',
        epname: '告白',
        singer: '王欣宇',
        src: 'http://music.163.com/song/media/outer/url?id=1325897842.mp3',
        coverImgUrl: 'https://pic.imgdb.cn/item/63dfdc1a4757feff3307dd35.jpg'
      }, {
        id: 3,
        title: '拾忆',
        epname: '拾忆',
        singer: '王欣宇',
        src: 'http://music.163.com/song/media/outer/url?id=545019506.mp3',
        coverImgUrl: 'https://pic.imgdb.cn/item/63dff0f14757feff33257352.png'
      }, {
        id: 4,
        title: '1234喜欢你',
        epname: '1234喜欢你',
        singer: '王欣宇',
        src: 'http://music.163.com/song/media/outer/url?id=1336228421.mp3',
        coverImgUrl: 'https://pic.imgdb.cn/item/63dff0f14757feff33257352.png'
      }, {
        id: 5,
        title: '云淡风轻',
        epname: '云淡风轻',
        singer: '王欣宇',
        src: 'http://music.163.com/song/media/outer/url?id=1364381428.mp3',
        coverImgUrl: 'https://pic.imgdb.cn/item/63dff0f14757feff33257352.png'
      }, {
        id: 6,
        title: '心跳小马达',
        epname: '心跳小马达',
        singer: '王欣宇',
        src: 'http://music.163.com/song/media/outer/url?id=1459513145.mp3',
        coverImgUrl: 'https://pic.imgdb.cn/item/63dff0f14757feff33257352.png'
      }, {
        id: 7,
        title: 'Do u love me',
        epname: 'Do u love me',
        singer: '王欣宇',
        src: 'http://music.163.com/song/media/outer/url?id=1817703165.mp3',
        coverImgUrl: 'https://pic.imgdb.cn/item/63dff0f14757feff33257352.png'
      }],
      min: 0, //slider最小值
      max: 0, //slider最大值:音频时长
      value: 0, //slider当前进度
      now: '00:00', //播放条当前时间
      long: '00:00', //音频总时长
    }
  },
  created: function() {
    var that = this;
    // 音频播放进度实时回调
    ba.onTimeUpdate(function() {
      // 当前播放的位置转化为整数
      var currentTime = parseInt(ba.currentTime);
      if (currentTime > 0 && currentTime != this.currentTime) {
        that.currentTime = currentTime;
        let now = that.formatS2M(currentTime);
        that.currentTime = currentTime;
        that.value = currentTime,
        that.now = now
        // console.log('当前时间', ba.currentTime, '总时长', ba.duration);
      }
      //   当前播放的长度
      if (ba.duration != that.max) {
        let long = that.formatS2M(ba.duration); //格式化时间
        that.duration = ba.duration;
        that.max = ba.duration,
        that.long = long
      }
    });
    ba.onCanplay(function() {
      // console.log('onCanplay', ba, '总时长', ba.duration);
      //   监听背景音频进入可播放状态事件
    });
    ba.onEnded(function() {
      // console.log('onEnd')
      that.setAudio(); //播放新音频
      //   监听背景音频自然播放结束事件
    });
    ba.onNext(function() {
      // console.log('onNext')
      that.playNext();
      //   监听用户在系统音乐播放面板点击下一曲事件（仅iOS）
    });
    ba.onPrev(function() {
      // console.log('onPrev')
      that.playPrev();
      //   监听用户在系统音乐播放面板点击上一曲事件（仅iOS）
    });
    ba.onEnded(function() {
      // console.log('onEnded')
      that.playNext();
    });
    ba.onStop(function() {
      //  监听背景音频停止事件
      // console.log('onStope')
    });
    ba.onWaiting(function() {
      // uni.$showMsg('onWaiting', '正在拼命加载中...')
      //   监听音频加载中事件。当音频因为数据不足，需要停下来加载时会触发
    });
    // 初始加载完成后，将音频列表放到globalData中，缓存播放列表
    app.globalData.list = that.list;
    that.setAudio(); //播放新音频
  },
  methods: {
    // 开始
    start() {
      ba.play();
    },
    // 暂停
    pause() {
      ba.pause();
    },
    // 新音频
    setAudio() {
      var app = getApp();
      var data = this.list[app.globalData.playIndex]
      this.max = 100;
      this.value = 0;
      this.coverImgUrl = data.coverImgUrl;
      this.title = data.title;
      this.singer = data.singer;
      this.epname = data.epname;
      this.currentTime = 0;
      this.duration = 0;
      
      ba.title = data.title;
      ba.epname = data.epname;
      ba.singer = data.singer;
      ba.coverImgUrl = data.coverImgUrl;
      ba.src = data.src;
    },
    // 下一首
    playNext() {
      var that = this;
      let playIndex = 0;
      if (app.globalData.playIndex < (app.globalData.list.length - 1)) {
        playIndex = app.globalData.playIndex + 1;
      }
      app.globalData.playIndex = playIndex;
      that.setAudio(); //新音频
    },
    // 播放上一首
    playPrev() {
      var that = this;
      var playIndex = app.globalData.list.length - 1;
      if (app.globalData.playIndex > 0) {
        playIndex = app.globalData.playIndex - 1;
      }
      app.globalData.playIndex = playIndex;
      that.setAudio(); //新音频
    },
    // 将秒格式化为如 02:05
    formatS2M(seconds) {
      var durationMinute = parseInt(seconds / 60);
      var durationSecond = parseInt(seconds % 60);
      if (durationSecond < 10) {
        durationSecond = '0' + durationSecond;
      }
      if (durationMinute < 10) {
        durationMinute = '0' + durationMinute;
      }
      return (durationMinute + ':' + durationSecond)
    }
  }
}
